package com.teng.smartdrawer.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.teng.smartdrawer.R;
import com.teng.smartdrawer.TaobaoActivity;
import com.teng.smartdrawer.bean.Component;
import com.teng.smartdrawer.utils.ImageLoadingConfig;

import java.util.List;

/**
 * Created by teng on 16/5/10.
 */
public class BuyAdapter extends BaseAdapter {

    private List<Component> mData;
    private Context context;

    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions imgOptions = ImageLoadingConfig.generateDisplayImageOptionsWithAnimationAndNoCorner(R.mipmap.ic_launcher);

    public BuyAdapter(List<Component> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View ret = null;

        if (ret != null) {
            ret = convertView;
        }else {
            ret = LayoutInflater.from(context).inflate(R.layout.item_buy , parent , false);
        }

        ViewHolder viewHolder = (ViewHolder) ret.getTag();

        if (viewHolder == null) {
            viewHolder = new ViewHolder();

            viewHolder.image_buy = (ImageView) ret.findViewById(R.id.image_buy);

            viewHolder.text_buy = (TextView) ret.findViewById(R.id.text_buy);

            viewHolder.text_name = (TextView) ret.findViewById(R.id.text_name);

            viewHolder.buy = (Button) ret.findViewById(R.id.buy);

            ret.setTag(viewHolder);
        }

        Component component = mData.get(position);

        String m_imgpc = component.getM_imgpc();

        if (m_imgpc == null){
            m_imgpc = "http://img2.pconline.com.cn/pconline/0706/19/1038447_34.jpg";
        }
        imageLoader.displayImage(m_imgpc, viewHolder.image_buy, imgOptions);

        viewHolder.text_buy.setText(Html.fromHtml("数量已低于<font color='red' size='24'>" + component.getM_fixed_num() + "</font>请及时购买"));

        viewHolder.text_name.setText(component.getM_name());

        viewHolder.buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context , TaobaoActivity.class);
                context.startActivity(intent);

//                Uri uri = Uri.parse("https://tsksm.tmall.com/category-686497010.htm?spm=a1z10.1-b.w5002-13877831174.4.rcl32Y&search=y&catName=%B5%E7%D7%D3%C4%A3%BF%E9%B4%F3%C8%AB");
//                Intent it = new Intent(Intent.ACTION_VIEW, uri);
//                context.startActivity(it);

            }
        });

        return ret;
    }


    private static class ViewHolder{

        public ImageView image_buy;

        public TextView text_buy , text_name;

        public Button buy;

    }

}
